package pl.un4giv3n.mightvsmagic.admin.action;

import java.util.Date;

import javax.mail.NoSuchProviderException;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import pl.un4giv3n.mightvsmagic.action.PersistenceAction;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserActivity;
import pl.un4giv3n.mightvsmagic.utils.ActivityLogUtil;
import pl.un4giv3n.mightvsmagic.utils.MailSender;

/**
 * @author andrzejewskia
 * delete player
 */
public class BanPlayerAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	private static final long ONE_DAY = 1000 * 60 * 60 * 24;
	private long playerId;
	private User player;
	private String reason;
	private long days;
	/**
	 * session user to be deleted
	 */
	private User admin;

	/* (non-Javadoc)
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		admin = getSessionUser(entityManager);
		if (admin == null) {
			addActionError("Zaloguj się");
			entityManager.close();	
			return LOGIN;
		}
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		player = entityManager.find(User.class, playerId);
		if(player == null){
			addActionError("Nieprawidłowe id usera");
			transaction.rollback();
			entityManager.close();	
			return ERROR;
		}
		boolean alreadyBanned = player.isBlocked();
		if(alreadyBanned){
			player.setBlocked(false);
			player.setBlockedUntil(null);
			player.setBlockReason(null);
			UserActivity adminActivity = ActivityLogUtil.createActivity(ActivityType.ADMIN_UNBAN_PLAYER, admin.getId()," Odblokowanie gracza: "+ player.toString()+", powód: " +reason);
			entityManager.persist(adminActivity);
			UserActivity activity = ActivityLogUtil.createActivity(ActivityType.ADMIN_UNBAN_PLAYER, player.getId()," Odblokowanie przez admina: "+ admin.toString()+", powód: " +reason);
			entityManager.persist(activity);
		}else{
			player.setBlocked(true);
			if(days > 0){
				player.setBlockedUntil(new Date(System.currentTimeMillis() + (days * ONE_DAY)));
			}
			player.setBlockReason(reason);
			UserActivity adminActivity = ActivityLogUtil.createActivity(ActivityType.ADMIN_BAN_PLAYER, admin.getId()," Blokada gracza: "+ player.toString()+", powód: " +reason + ", czas bana w dniach:="+days);
			entityManager.persist(adminActivity);
			UserActivity activity = ActivityLogUtil.createActivity(ActivityType.ADMIN_BAN_PLAYER, player.getId()," Blokada przez admina: "+ admin.toString()+", powód: " +reason + ", czas bana w dniach:="+days);
			entityManager.persist(activity);
		}
		entityManager.merge(player);
		entityManager.flush();
		transaction.commit();
		entityManager.close();
		try {
			if(alreadyBanned){
				MailSender.send(player.getEmail(), MailSender.UNBLOCK_USER_SUBJECT, MailSender.getUnBlockUserMessage(player, player.getId()+"", admin.getLogin(), reason));
			}else{
				MailSender.send(player.getEmail(), MailSender.BLOCK_USER_SUBJECT, MailSender.getBlockUserMessage(player, player.getId()+"", admin.getLogin(), reason));
			}
		} catch (NoSuchProviderException e) {
			LOG.error(e.getMessage(),e);
			addActionError("Błąd wysyłania wiadomości e-mail dotyczącej rejestracji!");
			return INPUT;
		}
		return SUCCESS;
	}

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public User getPlayer() {
		return player;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getBanned(){
		return player.isBlocked() ? "ZABLOKOWANY" : "AKTYWNY";
	}

	public long getDays() {
		return days;
	}

	public void setDays(long days) {
		this.days = days;
	}
	
}
